package com.common.tool;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;

/**
 * 日期公用类
 * 
 * @author lph
 * 
 */
@SuppressWarnings("unused")
public class DateUtil {

	public DateUtil() {
	}

	/**
	 * 把long形转换成指定日期格式的string
	 * 
	 * @param time
	 * @param format
	 * @return
	 */
	public static String parseFormatLongToStr(long time, String format) {
		SimpleDateFormat FORMAT_DATE_TIME = new SimpleDateFormat(format);
		return long2DateStr(time, FORMAT_DATE_TIME);
	}

	/**
	 * 把long形转换成指定日期格式的string
	 * 
	 * @param time
	 * @param format
	 * @return
	 */
	public static String long2DateStr(long time, SimpleDateFormat dateFmt) {
		java.sql.Date date = new java.sql.Date(time);
		if (date != null) {
			return dateFmt.format(date);
		}
		return "";

	}

	/**
	 * 把long形转换成指定日期格式的date，如果format是空则默认为yyyy-MM-dd HH:mm:ss格式
	 * 
	 * @param time
	 * @param format
	 * @return
	 */
	public static Date parseFormatLongToDate(long time, String format) {
		if (StringUtils.isEmpty(format)) {
			format = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat FORMAT_DATE_TIME = new SimpleDateFormat(format);
		return parseDateString(long2DateStr(time, FORMAT_DATE_TIME), format);
	}

	/**
	 * 将日值字符串转换成Timestamp类型
	 * 
	 * @param str
	 *            参数字符串
	 * @param format
	 *            格式
	 * @return String
	 */
	public static Timestamp parseFormatString(String str, String format) {
		if (str == null || str.equals(""))
			return null;
		try {
			DateFormat df = new SimpleDateFormat(format);
			Date dt = df.parse(str);
			Timestamp timestamp = new Timestamp(dt.getTime());
			return timestamp;
		} catch (Exception pe) {
		}
		Timestamp timestamp3;
		try {
			Timestamp timestamp1 = Timestamp.valueOf(str);
			return timestamp1;
		} catch (Exception e) {
			timestamp3 = null;
		}
		return timestamp3;
	}

	/**
	 * 将日值字符串转换成Date类型
	 * 
	 * @param str
	 *            参数字符串
	 * @param format
	 *            格式
	 * @return Date
	 */
	public static Date parseDateString(String str, String format) {
		if (str == null || str.equals(""))
			return null;
		Date date = null;
		try {
			DateFormat df = new SimpleDateFormat(format);
			date = df.parse(str);
		} catch (Exception pe) {
		}

		return date;
	}

	public static boolean isBeforeOrEqual(Date date1, Date date2) {
		if (date1.compareTo(date2) > 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 将日值Long字符串转换成Timestamp类型
	 * 
	 * @param str
	 *            参数字符串
	 * @return Timestamp
	 */
	public static Timestamp parseLongString(String str) {
		if (str == null || str.equals(""))
			return null;
		try {
			long time = Long.parseLong(str);
			Timestamp timestamp = new Timestamp(time);
			return timestamp;
		} catch (Exception ex) {
			return null;
		}

	}

	/**
	 * 将日期format成指定格式字符串
	 * 
	 * @param dt
	 *            日期
	 * @param format
	 *            格式
	 * @return String
	 */
	public static String formatDate(Date dt, String format) {
		if ((dt == null) || format == null) {
			return "";
		}
		String strDate = "";
		String s1;
		try {
			SimpleDateFormat DATA_FORMAT = new SimpleDateFormat(format);
			strDate = DATA_FORMAT.format(dt);
			String s = strDate;
			return s;
		} catch (Exception e) {
			s1 = null;
		}
		return s1;
	}

	/**
	 * 调整时间 将指定日期调整 时间
	 * 
	 * @param date
	 *            参数日期
	 * @param field
	 *            自定字段域
	 * @param amount
	 *            调整数量
	 * @param format
	 *            字符串格式
	 * @return String
	 */
	public static String getAdjustDate(Date date, int field, int amount, String format) {
		if ((date == null) || format == null) {
			return "";
		}
		Calendar can = Calendar.getInstance();
		can.setTime(date);
		can.add(field, amount);
		Date newDate = can.getTime();
		String dateStr = formatDate(newDate, format);
		return dateStr;
	}

	/**
	 * 调整时间 将指定日期调整 时间
	 * 
	 * @param date
	 *            参数日期
	 * @param field
	 *            自定字段域
	 * @param amount
	 *            调整数量
	 * @param format
	 *            字符串格式
	 * @return String
	 */
	public static Date getAdjustDate(Date date, int field, int amount) {
		if ((date == null)) {
			return null;
		}
		Calendar can = Calendar.getInstance();
		can.setTime(date);
		can.add(field, amount);
		Date newDate = can.getTime();
		return newDate;
	}

	/**
	 * 取2个时间相差的天数
	 * 
	 * @param date1
	 * @param date2
	 * @param isAbs
	 *            true:取绝对值 false:差值可能是负数
	 * @return
	 * @throws Exception
	 */
	public static long getBetweenDiffDay(Date date1, Date date2, boolean isAbs) throws Exception {
		long date1Value = date1.getTime();
		long date2Value = date2.getTime();
		long diff = (date1Value - date2Value) / (24 * 3600 * 1000);
		if (isAbs) {
			return Math.abs(diff);
		} else {
			return diff;
		}

	}

	public static long getBetweenDiffHour(Date date1, Date date2, boolean isAbs) throws Exception {
		long date1Value = date1.getTime();
		long date2Value = date2.getTime();
		long diff = (date1Value - date2Value) / (3600 * 1000);
		if (isAbs) {
			return Math.abs(diff);
		} else {
			return diff;
		}
	}

	/**
	 * 得到指定日期所在月的最后一天,如果给定时间是月末,返回值应该是下个月末
	 * 
	 * @param date
	 * @return Date
	 */
	public static Date getMonthEnd(Date date) {
		Calendar can = Calendar.getInstance();
		can.setTime(date);
		// 说明给定时间是月末,返回值应该是下个月末
		if (can.get(Calendar.DAY_OF_MONTH) == can.getActualMaximum(Calendar.DAY_OF_MONTH)) {
			can.add(Calendar.MONTH, 1);
		}
		can.set(Calendar.DAY_OF_MONTH, can.getActualMaximum(Calendar.DAY_OF_MONTH));
		can.set(Calendar.HOUR_OF_DAY, 23);
		can.set(Calendar.MINUTE, 59);
		can.set(Calendar.SECOND, 59);

		return can.getTime();
	}

	/**
	 * 得到指定日期所在月的第一天的0点0分0秒
	 * 
	 * @param date
	 * @return Date
	 */
	public static Date getCurrentMonthBegin(Date date) {
		Calendar can = Calendar.getInstance();
		can.setTime(date);
		// 说明给定时间是月末,返回值应该是下个月末

		can.set(Calendar.DAY_OF_MONTH, can.getActualMinimum(Calendar.DAY_OF_MONTH));
		can.set(Calendar.HOUR_OF_DAY, 00);
		can.set(Calendar.MINUTE, 00);
		can.set(Calendar.SECOND, 00);

		return can.getTime();
	}

	/**
	 * 返回指定天的开始
	 * 
	 * @param date
	 * @return
	 */
	public static Date getCurrentDayBegin(Date date) {
		Calendar can = Calendar.getInstance();
		can.setTime(date);
		can.set(Calendar.HOUR_OF_DAY, 00);
		can.set(Calendar.MINUTE, 00);
		can.set(Calendar.SECOND, 00);
		return can.getTime();
	}

	/**
	 * 返回指定天的结束
	 * 
	 * @param date
	 * @return
	 */
	public static Date getCurrentDayEnd(Date date) {
		Calendar can = Calendar.getInstance();
		can.setTime(date);
		can.set(Calendar.HOUR_OF_DAY, 23);
		can.set(Calendar.MINUTE, 59);
		can.set(Calendar.SECOND, 59);
		return can.getTime();
	}

	/**
	 * 得到指定日期所在月的第最后天的23点59分59秒
	 * 
	 * @param date
	 * @return Date
	 */
	public static Date getCurrentMonthEnd(Date date) {
		Calendar can = Calendar.getInstance();
		can.setTime(date);
		// 说明给定时间是月末,返回值应该是下个月末

		can.set(Calendar.DAY_OF_MONTH, can.getActualMaximum(Calendar.DAY_OF_MONTH));
		can.set(Calendar.HOUR_OF_DAY, 23);
		can.set(Calendar.MINUTE, 59);
		can.set(Calendar.SECOND, 59);

		return can.getTime();
	}

	/**
	 * 得到今天所在月的最后一天, 如果给定时间是月末,返回值应该是下个月末
	 * 
	 * @return Date
	 */
	public static Date getMonthEnd() {
		Date date = new Date();
		Calendar can = Calendar.getInstance();
		can.setTime(date);
		// 说明给定时间是月末,返回值应该是下个月末
		if (can.get(Calendar.DAY_OF_MONTH) == can.getActualMaximum(Calendar.DAY_OF_MONTH)) {
			can.add(Calendar.MONTH, 1);
		}
		can.set(Calendar.DAY_OF_MONTH, can.getActualMaximum(Calendar.DAY_OF_MONTH));
		can.set(Calendar.HOUR_OF_DAY, 23);
		can.set(Calendar.MINUTE, 59);
		can.set(Calendar.SECOND, 59);

		return can.getTime();
	}

	/**
	 * 得到指定日期所在周的的最后一天,如果传入的时间已经是周末,返回值应该是下个周末
	 * 
	 * @param date
	 * @return Date
	 */
	public static Date getWeekEnd(Date date) {
		Calendar can = Calendar.getInstance();
		can.setTime(date);
		// 说明传入的时间已经是周末,返回值应该是下个周末
		if (1 == can.get(Calendar.DAY_OF_WEEK)) {
			can.add(Calendar.WEEK_OF_MONTH, 1);
		}
		can.set(Calendar.DAY_OF_WEEK, can.getActualMaximum(Calendar.DAY_OF_WEEK));
		can.set(Calendar.HOUR_OF_DAY, 23);
		can.set(Calendar.MINUTE, 59);
		can.set(Calendar.SECOND, 59);
		can.add(Calendar.DAY_OF_MONTH, 1);
		return can.getTime();
	}

	/**
	 * 得到指定日期第二天的结束时间
	 * 
	 * @param date
	 * @return Date
	 */
	public static Date getTomorrowEnd(Date date) {
		Calendar can = Calendar.getInstance();
		can.setTime(date);
		can.set(Calendar.HOUR_OF_DAY, 23);
		can.set(Calendar.MINUTE, 59);
		can.set(Calendar.SECOND, 59);
		can.add(Calendar.DAY_OF_MONTH, 1);
		return can.getTime();
	}

	/**
	 * 获得指定天的昨天的最后时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getYesterdayEnd(Date date) {
		Calendar can = Calendar.getInstance();
		can.setTime(date);
		can.set(Calendar.HOUR_OF_DAY, 23);
		can.set(Calendar.MINUTE, 59);
		can.set(Calendar.SECOND, 59);
		can.add(Calendar.DAY_OF_MONTH, -1);
		return can.getTime();
	}

	/**
	 * 获得指定天的昨天的开始时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getYesterdayFirst(Date date) {
		Calendar can = Calendar.getInstance();
		can.setTime(date);
		can.set(Calendar.HOUR_OF_DAY, 00);
		can.set(Calendar.MINUTE, 00);
		can.set(Calendar.SECOND, 00);
		can.add(Calendar.DAY_OF_MONTH, -1);
		return can.getTime();
	}

	/**
	 * 设置特定时间的小时偏移量和天数偏移量。
	 * 
	 * @param date
	 *            指定的天
	 * @param hourOffset
	 *            小时偏移量 正数为增加小时，负数为减去小时
	 * @param dateOffset
	 *            天数偏移量 正数为增加天 ，负数为减去天
	 * @param startOrEnd
	 *            0为开始时间忽略小时偏移量，例：2008-01-10 00:00:00 1为结束时间忽略小时偏移量，例：2008-01-10
	 *            23:59:59 2为指定时间的偏移量时间 例：偏移量为-1 指定时间为2008-01-10 00:00:00
	 *            返回时间为：2008-01-09 23:00:00
	 * @return
	 */
	public static Date getAppointDateByOffset(Date date, int hourOffset, int dateOffset, int startOrEnd) {
		Calendar can = Calendar.getInstance();
		can.setTime(date);
		int hourOfDay = hourOffset;
		int minute = 0;
		int second = 0;
		if (startOrEnd == 1) {
			hourOfDay = 23;
			minute = 59;
			second = 59;
		} else if (startOrEnd == 2) {
			hourOfDay = hourOffset;
			minute = 0;
			second = 0;
		}

		can.set(Calendar.HOUR_OF_DAY, hourOfDay);
		can.set(Calendar.MINUTE, minute);
		can.set(Calendar.SECOND, second);
		can.add(Calendar.DAY_OF_MONTH, dateOffset);
		return can.getTime();
	}

	/**
	 * 得到现在时间所在周的的最后一天,如果传入的时间已经是周末,返回值应该是下个周末
	 * 
	 * @return Date
	 */
	public static Date getWeekEnd() {
		Date date = new Date();
		Calendar can = Calendar.getInstance();
		can.setTime(date);
		// 说明传入的时间已经是周末,返回值应该是下个周末
		if (1 == can.get(Calendar.DAY_OF_WEEK)) {
			can.add(Calendar.WEEK_OF_MONTH, 1);
		}
		can.set(Calendar.DAY_OF_WEEK, can.getActualMaximum(Calendar.DAY_OF_WEEK));
		can.set(Calendar.HOUR_OF_DAY, 23);
		can.set(Calendar.MINUTE, 59);
		can.set(Calendar.SECOND, 59);
		can.add(Calendar.DAY_OF_MONTH, 1);
		return can.getTime();
	}

	/**
	 * 将dt格式化成指定格式
	 * 
	 * @param dt
	 *            时间
	 * @param format
	 *            格式
	 * @return String
	 */
	public static String formatTime(Timestamp dt, String format) {
		if ((dt == null) || format == null) {
			return "";
		}
		String strDate = "";
		String s1;
		try {
			SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(format);
			strDate = DATE_FORMAT.format(dt);
			String s = strDate;
			return s;
		} catch (Exception e) {
			s1 = null;
		}
		return s1;
	}

	/**
	 * 取得偏移时间 nowDate:时间Date型; date偏移时间; day偏移天数; status为分钟量 0是返回时间为00:00:00
	 * ;1是返回为59:59:59; 2返回原始
	 * 
	 * 
	 */
	public static Date getOffsetTime(Date nowDate, int day, int status) {
		Calendar curCalendar = Calendar.getInstance();
		curCalendar.setTime(nowDate);
		long t = curCalendar.getTimeInMillis() + day * 24 * 60 * 60 * 1000L;
		curCalendar.setTimeInMillis(t);
		if (status == 0) {
			curCalendar.set(Calendar.HOUR_OF_DAY, 0);
			curCalendar.set(Calendar.MINUTE, 0);
			curCalendar.set(Calendar.SECOND, 0);
		} else if (status == 1) {
			curCalendar.set(Calendar.HOUR_OF_DAY, 23);
			curCalendar.set(Calendar.MINUTE, 59);
			curCalendar.set(Calendar.SECOND, 59);
		}
		return curCalendar.getTime();
	}

	public static void main(String[] args) {

		Date df = DateUtil.getYesterdayFirst(new Date());
		Date de = DateUtil.getYesterdayEnd(new Date());
		System.out.println(DateUtil.formatDate(df, "yyyy-MM-dd HH:mm:ss") + "-" + de);

	}



	/*
	 * String s1="2008-11-30"; // Date startDate=DateUtil.parseDateString(s1,
	 * "yyyy-MM-dd"); String s2="2008-11-13"; try { long
	 * long1=DateUtil.getBetweenDiffDay(DateUtil.parseDateString(s1,
	 * "yyyy-MM-dd"), DateUtil.parseDateString(s2, "yyyy-MM-dd"), false);
	 * System.out.println(long1); // for(int i=0;i<long1+1;i++){ // Date
	 * tmp=DateUtil.getAppointDateByOffset(startDate, 0, i, 2); //
	 * System.out.println(DateUtil.formatDate(tmp, "yyyy-MM-dd")); // } } catch
	 * (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
	 * 
	 * } //2009-11-02 jfDing start public final static String
	 * SHORT_DATE_FORMAT="yyyy-MM-dd HH:mm:ss";
	 * 
	 * public final static String ONLY_DATE_FORMAT="yyyy-MM-dd";
	 * //将字符串转换成指定格式的日期 public static Date StringToDate(String str,String
	 * format){ Date d=null; DateFormat format1 = new SimpleDateFormat(format);
	 * try { d=format1.parse(str); } catch (Exception e) { e.printStackTrace();
	 * return null; } return d; }
	 */
	// 2009-11-02 jfDing end
}
